// pages/family/family.js

const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    today: "2022-8-12",
    recordState: []
  },

  // 调用云函数查询室友（家庭成员）今日的健康上报情况
  reportQuery: function() {
    var that = this;
    wx.cloud.callFunction({
      name: "reportQuery",
      data: {
        building: app.globalData.household.building,
        room: app.globalData.household.room,
        date: new Date(that.data.today)
      },
      success: function(res) {
        if(res.result.errCode == 1) {
          console.log(res.result.errMsg);
        } else {
          console.log(res.result.errMsg);
          console.log(res.result);
          that.setData({
            recordState: res.result.data.recordState
          })
        }
      }
    })
  },

  // 调用云函数提醒未打卡成员
  reportRemind: function(_openId) {
    var that = this;
    wx.cloud.callFunction({
      name: "reportRemind",
      data: {
        _openId: _openId
      },
      success: function(res) {
        if(res.result.errCode == 0) {
          console.log("订阅消息发送成功");
          console.log(res.result.sendResult);
        } else if(res.result.errCode == 1) {
          console.log("未传入必要参数");
        } else {
          console.log(res.result.errMsg);
        }
      }
    })
  },

  // 提醒未上报的成员
  remind: function() {
    var that = this;
    var recordState = that.data.recordState;
    var len = recordState.length;
    for (var i = 0; i < len; i++) {
      if(!recordState[i].isComplete && recordState[i].openId != app.globalData.openId) {
        that.reportRemind(recordState[i].openId);
      }
    }
    wx.showModal({ // 弹出模态对话框
      title: '提示',
      content: '消息推送成功！',
      showCancel: false,
      success(res) {
        if (res.confirm) { // 用户点击确定后
          console.log('用户点击确定');
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    that.setData({
      today: app.getTime(),
    })
    that.reportQuery();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})